package com.isay.app.honey.ui.cash.history;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.isay.app.honey.R;
import com.isay.app.honey.manager.NetworkManager;
import com.isay.frameworklib.okhttp.ICommonCallback;
import com.isay.frameworklib.ui.BaseActivity;
import com.isay.frameworklib.utils.ToastUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 提现管理
 */
public class HistoryActivity extends BaseActivity implements View.OnClickListener {


    private RecyclerView mRecyclerView;
    protected HistoryAdapter mRecyclerAdapter;

    public static void launch(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, HistoryActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreateActivity(Bundle savedInstanceState) {
        setContentView(R.layout.activity_history);
        initView();
        initRecyclerView();
        initData();
    }

    @Override
    protected int getStatusBarColor() {
        return R.color.white;
    }

    private void initView() {
        ((TextView) findViewById(R.id.tv_title)).setText("提现记录");
        findViewById(R.id.iv_go_back).setOnClickListener(this);
    }

    private void initData() {
        cashHistory();
    }


    private void initRecyclerView() {
        mRecyclerView = findViewById(R.id.recycler_view);
        mRecyclerAdapter = new HistoryAdapter(this);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(mRecyclerAdapter);


    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_go_back:
                finish();
                break;
            default:
                break;
        }
    }


    private void cashHistory() {
        NetworkManager.cashHistory(new ICommonCallback() {
            @Override
            public void onSuccess(int ret, String msg, String content) {
                if (ret != 200) {
                    ToastUtils.show(msg);
                    return;
                }
                List<HistoryItemInfo> list = new ArrayList<>();
                try {
                    JSONArray jsonArray = new JSONArray(content);
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        HistoryItemInfo item = new HistoryItemInfo();
                        item.setMoney(jsonObject.optString("money"));
                        item.setState(jsonObject.optString("status"));
                        item.setTime(jsonObject.optString("addtime"));
                        list.add(item);
                    }
                } catch (Exception e) {
                    onFailure(e);
                }
                if (list != null && list.size() > 0) {
                    mRecyclerAdapter.addData(false, list);
                    mRecyclerAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(Exception e) {

            }
        });
    }


}
